<?php

class Mail
{
    public $mail;
    public $charset = "UTF-8";
    public $encoding = 'base64';
    public $issmtp = true;
    public $smtpauth = true;
    public $smtpsecure = '';
    public $host;
    public $port = 25;
    public $username;
    public $password;
    public $fromname;
    
    /***
     * 初始化
     */
    function __construct()
    {
        global $root;
        require $root.'_core/mail/class.phpmailer.php';          
        
        $this->mail = new PHPMailer();
    }
    
    function init()
    {         
        $this->mail->CharSet = $this->charset;
        
        $this->mail->Encoding = $this->encoding;
        
        if($this->issmtp){ $this->mail->IsSMTP(); }
        
        if($this->smtpauth){ $this->mail->SMTPAuth = true; }
        
        $this->mail->SMTPSecure = $this->smtpsecure;
        
        $this->mail->Host = $this->host;
        
        $this->mail->Port = $this->port;
        
        $this->mail->Username = $this->username;
        
        $this->mail->Password = $this->password;
        
        $this->mail->From = $this->username;
        
        $this->mail->FromName = $this->fromname;
        
        return $this;
    }
    
    function setContent($subject,$body,$ishtml=false)
    {
        $this->mail->IsHTML = $ishtml;
        $this->mail->Subject = $subject;
        $this->mail->Body = $body;
        $this->mail->AltBody = $body;
        return $this;
    }
    
    function setAddress($array)
    {
        if(count($array)>0)
        {
            foreach($array as $k => $v)
            {
                $this->mail->AddAddress($k,$v);
            }
        }
        return $this;
    }
    
    function setAddAttachment($array)
    {
        
        if(count($array)>0)
        {
            foreach($array as $k => $v)
            {
                $this->mail->AddAttachment($k,$v);
            }
        }
        return $this;
    }
    
    function send()
    {
        if(!$this->mail->Send())
        {
            echo $this->mail->ErrorInfo;
        }
        else
        {
            return true;
        } 
    }
}